package com.oms.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.oms.module.admin.domain.entity.AdminEntity;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Objects;

/**
 * @Author zhanghailong
 * @Description TODO
 * @Date 19:31 2020/3/26
 */
@Configuration
@MapperScan(basePackages= {"com.oms.module.*.dao"})

public class MybatisPlusConfig {
    /**
     *   mybatis-plus分页插件
     * @return  {@code<PaginationInterceptor>}
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }

    /**
     * 元数据处理，处理更新日期，插入日期，更新人等
     * @return {@code MetaObjectHandler}
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {

            @Override
            public void updateFill(MetaObject metaObject) {
                if(metaObject.hasGetter("updateDate")) {
                    Object value = metaObject.getValue("updateDate");
                    if(Objects.isNull(value)) {
                        if(metaObject.hasSetter("updateDate")) {
                            metaObject.setValue("updateDate", LocalDateTime.now( ZoneId.systemDefault()));
                        }
                    }
                }
                if(metaObject.hasGetter("updateUserId")) {
                    Object value = metaObject.getValue("updateUserId");
                    if(Objects.isNull(value)) {
                        if(metaObject.hasSetter("updateUserId")) {
                            metaObject.setValue("updateUserId", ((AdminEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
                        }
                    }
                }
//                if(metaObject.hasGetter("updateUserRole")) {
//                    Object value = metaObject.getValue("updateUserRole");
//                    if(Objects.isNull(value)) {
//                        if(metaObject.hasSetter("updateUserRole")) {
//                            metaObject.setValue("updateUserRole", UserUtil.getCurrentUserRole());
//                        }
//                    }
//                }
            }

            @Override
            public void insertFill(MetaObject metaObject) {
                if(metaObject.hasGetter("createDate")) {
                    Object value = metaObject.getValue("createDate");
                    if(Objects.isNull(value)) {
                        if(metaObject.hasSetter("createDate")) {
                            metaObject.setValue("createDate", LocalDateTime.now(ZoneId.systemDefault()));
                        }
                    }
                }
                if(metaObject.hasGetter("createUserId")) {
                    Object value = metaObject.getValue("createUserId");
                    if(Objects.isNull(value)) {
                        if(metaObject.hasSetter("createUserId")) {
                            metaObject.setValue("createUserId", ((AdminEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
                        }
                    }
                }
//                if(metaObject.hasGetter("createUserRole")) {
//                    Object value = metaObject.getValue("createUserRole");
//                    if(Objects.isNull(value)) {
//                        if(metaObject.hasSetter("createUserRole")) {
//                            metaObject.setValue("createUserRole", UserUtil.getCurrentUserRole());
//                        }
//                    }
//                }
                updateFill(metaObject);
            }
        };
    }
}
